using System;
using System.IO;
using System.Windows;
using SharpDropBoxClient;

namespace DropBoxTool
{
    public class NavigatableDropBoxFolderItem : DBoxListing
    {
        public const string kImageFileTypes = ".JPG,.GIF,.PNG,.BMP,.TIF";
        public const string kMusicFileTypes = ".MP3,.WMA,.WAV,.M4A";
        public const string kMovieFileTypes = ".WMV,.3GP,.3G2,.MP4,.M4V";

        public static string[] imageTypes = kImageFileTypes.Split(',');
        public static string[] musicTypes = kMusicFileTypes.Split(',');
        public static string[] movieTypes = kMovieFileTypes.Split(',');

        public static NavigatableDropBoxFolderItem FromBaseType(DBoxListing item)
        {
            return new NavigatableDropBoxFolderItem
            {
                Contents = item.Contents,
                FullPath = item.FullPath,
                Hash = item.Hash,
                Icon = item.Icon,
                IsDirectory = item.IsDirectory,
                LastModified = item.LastModified,
                Root = item.Root,
                Size = item.Size,
                SizeInBytes = item.SizeInBytes,
                ThumbnailExists = item.ThumbnailExists
            };
        }

        public string Uri
        {
            get
            {
                return "/BrowseFiles.xaml?path=" + base.FullPath + ((this.IsDirectory && !base.FullPath.EndsWith("/")) ? "/" : "");
            }
        }

        public Visibility ShowAsLink
        {
            get
            {
                return IsDirectory ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility ShowAsNonLink
        {
            get
            {
                return !IsDirectory ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public Visibility DiplayButtonVisibility
        {
            get
            {
                if (IsDirectory)
                    return Visibility.Collapsed;
                return IsViewableType ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public bool IsViewableType
        {
            get
            {
                return GetViewerType() != ViewerTypes.NotSupported;
            }
        }

        public string FilePath
        {
            get
            {
                return Path.GetDirectoryName(FullPath) + @"\";
            }
        }

        public string FileNameWithoutExtension
        {
            get
            {
                return Path.GetFileNameWithoutExtension(FullPath);
            }
        }

        public string DisplayUri
        {
            get
            {
                switch (GetViewerType())
                {
                    case ViewerTypes.Image:
                        return "/ImageViewer.xaml?file=" + FullPath;
                        break;
                    case ViewerTypes.Music:
                        return "/MusicPlayer.xaml?file=" + FullPath;
                        break;
                    case ViewerTypes.Video:
                        return "/VideoPlayer.xaml?file=" + FullPath;
                        break;
                    //case ViewerTypes.Comic:
                    //    return "/ComixViewer.xaml?file=" + FullPath;
                    //    break;

                    default:
                        return "";
                        break;
                }
                return "";
            }
        }

        public ViewerTypes GetViewerType()
        {
            var extension = (Path.GetExtension(base.FullPath).ToUpperInvariant());
            if (Array.IndexOf(imageTypes, extension) != -1)
            {
                return ViewerTypes.Image;
            }
            else if (Array.IndexOf(musicTypes, extension) != -1)
            {
                return ViewerTypes.Music;
            }

            else if (Array.IndexOf(movieTypes, extension) != -1)
            {
                return ViewerTypes.Video;
            }
            else
                return ViewerTypes.NotSupported;
        }
    }

    public enum ViewerTypes
    {
        NotSupported,
        Image,
        Music,
        Video,
        Comic
    }
}